package com.songminju.jsong_ioc.loadclass;

import java.io.File;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

public class LoadClassUtil {
	private Logger logger = Logger.getLogger(this.getClass());
	public List<String> scanClassList(String packStr){
		URL basePathUrl = this.getClass().getResource("/");
		logger.debug("basePathUrl:"+basePathUrl.getFile());
		String path = basePathUrl.getFile();
		path += packStr.replace(".",File.separator);
		File file = new File(path);
		List<String> results = new LinkedList<>();
		findClass(basePathUrl.getFile(),file, results);		
		return results;
	}
	private void findClass(String basePath,File file,List<String> results) {
		if(file.isFile()) {
			String fileName = file.getAbsolutePath();
			if(fileName.endsWith(".class")) {
				int x = fileName.length()-6;
				fileName = fileName.substring(basePath.length(),x);
				fileName = fileName.replace(File.separator,".");
				results.add(fileName);
			}
			return;
		}else if(file.isDirectory()) {
			for(File f:file.listFiles()) {
				findClass(basePath,f, results);
			}
		}
	}
	
}
